<?php

use Drupal\newmenu\newmenuRetrieveController;
use Drupal\newmenu\newmenuServicesController;

function _newmenu_retrieve($action) {
  try {
    $query = drupal_get_query_parameters();
    $action = strtr($action, '-', '_');
    $class = newmenuRetrieveController::class;
    $reflection = new ReflectionMethod($class, $action);
    $params = [];
    foreach ($reflection->getParameters() AS $arg) {
      if (isset($query[$arg->name])) {
        $params[$arg->name] = $query[$arg->name];
      }
      else {
        if (!$arg->isOptional()) {
          return [
            'code' => 1,
            'msg' => "{$arg->name} 必需填写",
          ];
        }
      }
    }
    return call_user_func_array([new $class, $action], $params);
  } catch (ReflectionException $e) {
    return [
      'code' => 1,
      'msg' => $e->getMessage(),
    ];
  } catch (Exception $e) {
    watchdog_exception(__FUNCTION__, $e);
    return [
      'code' => 1,
      'msg' => '系统错误',
    ];
  }
}

function _newmenu_services_set_user_info($iv, $encrypted) {
  $controller = new newmenuServicesController();
  return $controller->set_user_info($iv, $encrypted);
}

function _newmenu_services_set_user_phone($iv, $encrypted) {
  $controller = new newmenuServicesController();
  return $controller->set_user_phone($iv, $encrypted);
}
